﻿using Microsoft.Extensions.DependencyInjection;
using System;
namespace XBD.Interface
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var order = new Order 
            {
                Id = 123,
                DatePlaced = DateTime.Now,
                TotalPrice = 100f
            };
            //配置IOC
            ServiceCollection services = new ServiceCollection();
            services.AddSingleton<IOrderProcessor, OrderProcessor>();
            //singleton, 单例模式
            //scoped, 作用域模式
            //tansient 瞬时模式，用完即丢的服务
            services.AddScoped<IShippingCalculator,DoubleElevenShippingCalculator>();

            //提取服务
            IServiceProvider serviceProvider = services.BuildServiceProvider();

            // 通过接口，从IOC中找出订单处理器
            var orderProcess = serviceProvider.GetService<IOrderProcessor>();
            //var orderProcessor2 = serviceProvider.GetService<IOrderProcessor>();

            // 处理订单
            orderProcess?.Process(order);

            Console.Read();
/*
            IShippingCalculator doubleEleven = new 
                DoubleElevenShippingCalculator();
            IShippingCalculator putong = new ShippingCalculator();
            var orderProcessor = new OrderProcessor(doubleEleven);
            if (DateTime.Now != new DateTime(2050,11,11)) 
            {
                orderProcessor = new OrderProcessor(putong);
            }

            orderProcessor.Process(order);*/

           // Console.WriteLine(order.Id);
        }

    }
}
